const router = require('koa-router')()
const getDevice = require('../modules/getDevice')
const addDevice = require('../modules/addDevice')
const removeDevice = require('../modules/removeDevice')
const getTemplate = require('../modules/getTemplate')
const addTemplate = require('../modules/addTemplate')
const removeTemplate = require('../modules/removeTemplate')
const setUserSchedule = require('../modules/setUerSchedule')
const setSchedule = require('../../tools/setSchedule')


//用户设备
router.get('/userDevices', async (ctx, next) => {
    const { username } = ctx.query;
    const result = await getDevice(username);
    ctx.body = result;
    await next();
})

router.post('/userDevices', async (ctx, next) => {
    const info = ctx.request.body
    const result = await addDevice(info);
    ctx.body = result
    await next();

})

router.delete('/userDevices', async (ctx, next) => {
    const { username,deviceId } = ctx.query
    const result = await removeDevice({username,deviceId})
    ctx.body = result;
    await next();
})

//用户数据模板
router.get('/userTemplate', async (ctx, next) => {
    const { username } = ctx.query;
    const result = await getTemplate({username});
    ctx.body = result;
    await next();
})

router.post('/userTemplate', async (ctx, next) => {
    const info = ctx.request.body
    const result = await addTemplate(info);
    ctx.body = result;
    await next();

})

router.delete('/userTemplate', async (ctx, next) => {
    const { username,templateId } = ctx.query
    const result = await removeTemplate({username,templateId})
    ctx.body = result;
    await next();
})

//用户定时任务
//传入deviceSlvaer,deviceId,username
router.post('/userSchedule', async (ctx,next) =>{
    const info = ctx.request.body
    const result = await setUserSchedule(info);
    ctx.body = result;
    await next();
})

//删除设备所有定时任务
//传入info{ deviceId,slaverLength //设备长度}
router.delete('/userSchedule', async (ctx,next) =>{
    const info = ctx.query
    const result = await setSchedule.cancelDeviceSchedule(info);
    ctx.body = result;
    await next();
})




module.exports = router;